package com.jasonli.controller;

import com.jasonli.domain.ProductType;
import com.jasonli.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/types")
public class ProductTypeController {

    @Autowired
    private ProductTypeService typeService;

    @PostMapping
    public Result save(@RequestBody ProductType type) {
        boolean flag = typeService.save(type);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    @DeleteMapping("/{tid}")
    public Result delete(@PathVariable Integer tid) {
        boolean flag = typeService.delete(tid);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    @PutMapping
    public Result update(@RequestBody ProductType type) {
        boolean flag = typeService.update(type);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    @GetMapping("/{tid}")
    public Result getByTid(@PathVariable Integer tid) {
        ProductType type = typeService.getByTid(tid);
        Integer code = type != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code, type);
    }

    @GetMapping
    public Result getAll() {
        List<ProductType> typeList = typeService.getAll();
        Integer code = typeList != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code, typeList);
    }

}
